package com.majorty.mb.controller;

import com.majorty.mb.controller.request.UserRequest;
import com.majorty.mb.controller.response.CodeEnum;
import com.majorty.mb.controller.response.PaginationResult;
import com.majorty.mb.dao.UserDao;
import com.majorty.mb.dao.entity.UserDO;
import com.majorty.mb.service.UserService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 前端控制器
 * @author zhangshuai
 * @date 2019-09-21 23:13
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
    // http://localhost:8090/user/getUserList
    @Resource(name = "userServiceImpl")
    private UserService userService;

    @Resource(name = "userDao")
    private UserDao userDao;

    /**
     * 返回带分页的相应结果
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserList")
    public String getUserList(@RequestBody UserRequest request){
        PaginationResult<UserDO> result = userService.getUserList(request);
        return generateResult(result, CodeEnum.SUCCESS);
    }

    /**
     * 普通相应结果
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getUser/{id}")
    public String getUserList(@PathVariable("id")Integer id){
        UserDO result = userService.selectById(id);
        return generateResult(result,CodeEnum.SUCCESS);
    }

    /**
     * 成功响应提示
     * @return
     */
    @ResponseBody
    @GetMapping("/getSuccess")
    public String getSuccess(){
        return generateResponse(CodeEnum.SUCCESS);
    }

    /**
     * 失败响应提示
     * @return
     */
    @ResponseBody
    @GetMapping("/getError")
    public String getError(){
        return generateResponse(CodeEnum.ERROR);
    }
}
